<style scoped lang="less">
.threshold-ipt{
    padding: 0 10px;
    height: 32px;
    line-height: 32px;
    width: 100px;
    background: #1E3C88;
    border:1px solid transparent;
    &:focus{
        border-color: rgba(255,255,255,.4);
    }
}
</style>
<template>
	<div class="threshold">
		<el-table 
			:data="dataList" 
			style="width: 100%"
			v-loading="loading">
		    <el-table-column prop="item" label="检测项"></el-table-column>
		    <el-table-column prop="unit" label="单位"></el-table-column>
		    <el-table-column prop="min" label="预警最小值">
                <template slot-scope="scope">
                    <input class="threshold-ipt" type="number" :value="scope.row.min" @blur="blurMin($event,scope.$index)" />
                </template>
            </el-table-column>
		    <el-table-column prop="max" label="预警最大值">
		    	<template slot-scope="scope">
                    <input class="threshold-ipt" type="number" :value="scope.row.max" @blur="blurMax($event,scope.$index)"/>
                </template>
		    </el-table-column>
		</el-table>
	</div>
</template>
<script>
export default {
	name: 'iotBlk',
	props: {
		number: {
			type: String,
			default: ''
		}
	},
	computed: {},
	watch: {},
	data () {
		return {
            loading: false,
            dataList: []
		}
	},
	methods: {
		getData () {
            this.$post('/fireFightingIOT/itemSetup/getItemSetupList', {number: this.number}).then(res => {
            	this.loading = false
                this.dataList = res.data
            }, (err) =>{
            	this.loading = false
				if(err.msg){
					this.$message.error(err.msg);
				}
            })
        },
        blurMin (e,index) {
            let value = e.target.value
            let activeItem = this.dataList[index]
            activeItem.min = value
        },
		blurMax (e,index) {
			let value = e.target.value
            let activeItem = this.dataList[index]
            activeItem.max = value
		},
		submitAction () {
			let items = this.dataList.map(item=>{
                return {
                    item: item.item,
                    min: item.min,
                    max: item.max,
                    unit: item.unit
                }
			})
			for(let i=0;i<items.length;i++){
				if(items[i].min.length==0 || items[i].max.length==0){
					this.$message.error('所有阈值项都为必填');
					return
				}
			}
			// return
            this.$post('/fireFightingIOT/itemSetup/insertItemSetup', {item: items, number: this.number}).then(res => {
				this.$emit('success')
            	this.$message.success('修改成功');
            }, (err) =>{
				this.loading = false
				this.$emit('fail')
				if(err.msg){
					this.$message.error(err.msg);
				}
            })
		}
	},
	created () {
		this.getData()
	},
}
</script>